package com.mt.roll.report.controller;

import com.mt.roll.core.base.BaseController;
import com.mt.roll.core.constants.CommonConstant;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.validator.groups.PageGroup;
import com.mt.roll.core.validator.groups.UpdateGroup;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.report.params.ReportSubstrateParam;
import com.mt.roll.report.params.ReportSubstrateQueryParam;
import com.mt.roll.report.service.ReportSubstrateSerice;
import com.mt.roll.report.vo.ReportSubstrateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author: yulianchen
 * @Date: 2022/3/4 9:55
 * @Version 1.0
 */
@RestController
@RequestMapping(CommonConstant.API_MODULE_REPORT + "report-substrate")
@Slf4j
@Api(value = "当前库存基板报表", tags = "当前库存基板报表", hidden = false)
public class ReportSubstrateContorller extends BaseController {
    @Autowired
    private ReportSubstrateSerice reportSubstrateSerice;


    @PostMapping(value = "list")
    @ApiOperation(value = "获取当前存储箱对应基板列表")
    private R<List<ReportSubstrateVo>> list(@Validated({Default.class}) @RequestBody ReportSubstrateQueryParam reportSubstrateQueryParam) {
        return reportSubstrateSerice.list(reportSubstrateQueryParam);
    }


    @PostMapping(value = "page")
    @ApiOperation(value = "获取当前存储箱对应基板分页报表")
    private R<PageVo<ReportSubstrateVo>> page(@Validated({PageGroup.class, Default.class}) @RequestBody ReportSubstrateQueryParam reportSubstrateQueryParam) {
        return reportSubstrateSerice.page(reportSubstrateQueryParam);
    }


    @PostMapping(value = "update")
    @ApiOperation(value = "更新基板信息")
    private R<IdVo> update(@Validated({UpdateGroup.class, Default.class}) @RequestBody ReportSubstrateParam reportSubstrateParam) {
        return reportSubstrateSerice.update(reportSubstrateParam);
    }


    @PostMapping(value = "delete")
    @ApiOperation(value = "删除基板信息")
    private R<IdVo> delete(@Validated @RequestBody IdParam idParam) {
        return reportSubstrateSerice.delete(idParam);
    }


    @PostMapping("excel-download")
    @ApiOperation(value = "导出当前存储箱对应基板列表",produces="application/octet-stream")
    public void excelDownload(HttpServletRequest request, HttpServletResponse response, ReportSubstrateQueryParam reportSubstrateQueryParam) throws IOException {
        R<List<ReportSubstrateVo>> rExcelData = reportSubstrateSerice.list(reportSubstrateQueryParam);
//        List<ReportSubstrateVo> excelData = reportSubstrateSerice.page(ReportSubstrateQueryParam).getData().getRecords();
        List<ReportSubstrateVo> ExcelData = rExcelData.getData();
        //sheetName：工作簿名称
        String sheetName = URLEncoder.encode("存储箱对应基板报表", "utf-8");
        //fileName:表名
        String fileName = URLEncoder.encode("SubstrateReport.xls", "utf-8");
        com.mt.roll.core.utils.ExcelExportUtil.exportXssfExcel(fileName, sheetName, ReportSubstrateVo.class, ExcelData, request, response);
    }
}
